<template>
  <div class="loginBox" v-if="isShowLogin">
    <div class="mask" @click="changeIsShowLogin"></div>
    <div class="content">
      <div class="close" @click="changeIsShowLogin"></div>
      <div class="nav">
        <span :class="{ current: isShow }" @click="isShow = true"
          >手机号码登录</span
        >
        |
        <span :class="{ current: !isShow }" @click="isShow = false"
          >微信扫码登录</span
        >
      </div>
      <div class="login">
        <div class="login__content" v-show="isShow">
          <div>
            <input
              type="text"
              class="ipt"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div>
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="arr"
            ></slide-verify>
          </div>
          <div>
            <input
              type="text"
              class="ipt"
              placeholder="请输入短信验证码"
              v-model.trim="code"
            />
            <input
              class="btn btncode"
              :class="{ active: count != 60 }"
              @click="getCode"
              :disabled="count != 60"
              type="button"
              :value="count == 60 ? '获取验证码' : count + 's后再获取'"
            />
          </div>
          <div>
            <button class="btn btnlogin" @click="submitFn">登录</button>
          </div>
        </div>
        <div v-show="!isShow">验证码</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { sendsmsAPI, phoneReginAPI } from "../utils/api";
import { checkedPhoneAndSlide } from "../utils/check";
export default {
  name: "Login",
  data() {
    return {
      isShow: true,
      msg: "向右滑动",
      arr: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-02.150d92a1.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-04.afadcbdf.png"),
      ],
      phoneNumber: "",
      count: 60,
      code: "",
    };
  },
  computed: {
    ...mapState("loginModule", ["isShowLogin"]),
  },
  methods: {
    ...mapMutations("loginModule", ["changeIsShowLogin"]),
    ...mapMutations("showCartModule", ["changeShowCart"]),
    ...mapActions("showToast", ["asyncChangeStatus"]),
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    async submitFn() {
      if (!checkedPhoneAndSlide(this.phoneNumber, this.msg)) {
        return;
      }
      if (this.code.length == 4) {
        const res = await phoneReginAPI({
          phone: this.phoneNumber,
          verifyCode: this.code,
        });
        if (res === false) return;
        this.asyncChangeStatus({ content: "登录成功", type: "success" });
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        this.changeIsShowLogin();
        this.changeShowCart(true);
      } else {
        this.asyncChangeStatus({ content: "请输入正确的验证码", type: "info" });
      }
    },
    // 获取验证码判断
    async getCode() {
      if (!checkedPhoneAndSlide(this.phoneNumber, this.msg)) {
        return;
      }
      let res = await sendsmsAPI({
        phone: this.phoneNumber,
      });
      // 拦截器返回false代表有错误终止函数
      if (res === false) return;
      this.asyncChangeStatus({ content: "发送成功", type: "success" });
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          clearInterval(timer);
          this.count = 60;
        }
      }, 1000);
    },
  },
};
</script>

<style lang="less" scoped>
.loginBox {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .content {
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .close {
      position: absolute;
      top: 18px;
      right: 65px;
      width: 22px;
      height: 22px;
      background: url("../assets/img/close.png");
      cursor: pointer;
    }
    .nav {
      font-size: 18px;
      text-align: center;
      padding: 40px 0 30px;

      span {
        cursor: pointer;
        color: gray;
        &.current {
          color: black;
        }
      }
    }
    .login {
      width: 355px;
      height: 252px;
      margin: 0 auto;
      &__content {
        & > div {
          display: flex;
          margin-bottom: 20px;
          //   height: 50px;
        }
        .ipt {
          box-sizing: border-box;
          height: 50px;
          flex: 1;
          outline: none;
          text-indent: 1em;
        }
        .btn {
          background-color: #0a328e;
          height: 50px;
          color: #fff;
          font-size: 16px;
          border: none;
          cursor: pointer;
          &.active {
            background-color: #ccc;
          }
        }
        .btncode {
          width: 100px;
          margin-left: 10px;
        }
        .btnlogin {
          width: 100%;
        }
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>>